<?php
/**
 * PHP versions 4 and 5
 * MagicPHP
 * Licensed under The Open Group Test Suite License
 * Redistributions of files must retain the above copyright notice.
 * @copyright     Copyright 2011
 * @link
 * @package       Magic Lib
 * @since         MaginPHP 1.0
 * @license
 */
?>
<?php
/**
 * @desc : MagicPHP class contain basic functionality 
 * It is reponsible for all the operation of the framework. 
 */
class Magic  {
    /**
     * @desc : This the static variable for model extention
     * @var type : public static
     */
    public static $m_ext = 'model';
    /**
     * @desc : This is the static variable for controller extenstion
     * @var type : public static
     */
    public static $c_ext = 'controller';
    /**
     * @desc : static variable for php extention
     * @var type : public static
     */
    public static $ext  = '.php';
    /**
     * @desc : This is the static variable contain [.] dot
     * @var type : public static
     */
    public static $dot = '.';
    /**
     * @desc : This variable used to save cache file extention 
     * @var type 
     */
    public static $cacheExt = '.html' ;
    
    /**
     * @desc : This is the static variable for instance of the class 
     * @var type : public static 
     */
    public static $_instance ; 
    /**
     * @desc : This is the only static method of the class 
     * Which contain the static instance variable of the class .
     * Each public methad of the class can access vai this method. 
     */
    public static function  callMethod(){
        self::$_instance = new self ; 
        return self::$_instance ;
    }
    /**
     * @desc : This the comman function for import file in framework
     * @param type $file:name of the file without extention
     * @param type $extention : file extention
     * @param type $debug : ture / flase 
     */
    public  function import($file,$extention = NULL ,$debug = false){
        $extention != ''  ? $extention = self::$dot.$extention : $extention = '.php';
        $fiileName = $file.$extention;
        if($debug == true) { echo $fiileName."<br/>" ; }
        if(file_exists($fiileName)) {
            include_once "$fiileName";
        } else {
              echo  new ErrorException($fiileName. "Is not exist.");
        }   
    }
    /**
     * @desc : This is the comman function for import magicphp library files into porject.
     * @param type $file:name of the file without extention
     * @param type $extention : file extention
     * @param type $debug : ture / flase 
     */
    public function importLibarary($file,$extention=NULL,$debug = false){
        $this->import(MAGICPHP_LIB_PATH.$file,$extention,$debug);
    }
    /**
     * @desc : This is the comman function to define the model class of the framework.
     * @param type $element: value of the key of array 
     * @param type $queryString : The query string of project 
     * @return string : name of model / view / controller
     */
    public  function defineModel($element,$queryString){
        $qString = $_GET["q"]; 
        if(FLAG == 0) {
            $str = str_replace('index.php/','',$qString);
            empty($str) ? $count=0 : $count = sizeof(explode('/',$str)) ;
            $mvc = explode("/", $str);
            if($count == 0  || $mvc[0] == 'index.php' ) {
                array_push($mvc[0]=DEFAULT_MODEL);
                array_push($mvc[1]=DEFAULT_CONTROLLER);
                array_push($mvc[2]=DEFAULT_ACTION);
            } elseif($count>=1 && $count<2) {
                array_push($mvc[1]=DEFAULT_CONTROLLER);
                array_push($mvc[2]=DEFAULT_ACTION);
            } elseif($count>=2 && $count<3) {
                array_push($mvc[2]=DEFAULT_ACTION);
            }
        } elseif (FLAG == 1)  {
            $pattern = '/\//i';
            if(preg_match($pattern, $qString)){
                $mvc = explode("/",$qString);
                $mvc[1] != '' ? $count = sizeof($mvc) : $count = 0;
            } else {
                $count = 0 ;
            } 
            if($count == 0) {
                array_push($mvc[0]=DEFAULT_MODEL);
                array_push($mvc[1]=DEFAULT_CONTROLLER);
                array_push($mvc[2]=DEFAULT_ACTION);
            } elseif($count >=1 && $count < 2){
                array_push($mvc[1]=DEFAULT_CONTROLLER);
                array_push($mvc[2]=DEFAULT_ACTION);
            } elseif($count >=2 && $count  < 3 ){
                array_push($mvc[2]=DEFAULT_ACTION);
            }
        }
   
        return $mvc[$element] ; 
    }
    /**
     * @desc : Get the name of the controller 
     * @return type : name of the controller
     */
    public  function getController($flag =false){
       return $flag == true ? self::defineModel(1,$_SERVER["QUERY_STRING"]) : 
           self::defineModel(1,$_SERVER["QUERY_STRING"])."_".self::$c_ext ;
     //  return self::defineModel(1,$_SERVER["QUERY_STRING"])."_".self::$c_ext;
    }
    /**
     * @desc : Get the name of the model .
     * @return type : Name of the model .
     */
    public  function getModel(){
       return self::defineModel(0,$_SERVER["QUERY_STRING"])."_".self::$m_ext;
    }
    /**
     * @desc : This function used to create dynamic object of any class
     */
    public  function createObj(){
        $name = self::getModel();
        $$name = new $name ;
    }
    /**
     * @desc: This function is used to get the view of the framework.
     * @return type : name of the view file .
     */
    public  function getView(){
       return self::defineModel(2,$_SERVER["QUERY_STRING"]);
    }
    /**
     * @desc : This function is used to return view action name 
     * @return type : view action name
     */
    public  function getViewAction(){
        return self::defineModel(2,$_SERVER["QUERY_STRING"])."Controller";
    }
    /**
     * @desc : Get the name of the application directory
     * @return type string 
     */
    public  function getAppDirectory(){
         return self::defineModel(0,$_SERVER["QUERY_STRING"]);
    } 
    /**
     * @desc : Used to check if any controller class extends base class or note 
     * @param type $class: name of the controller class 
     * @return boolean : true / flase 
     */
    public function getBaseClass($class){
        
        
        if(get_parent_class($class) == 'base_controller' ){
            return true;
        } else {
            return true;
        }
    }
    /**
     *@desc : This function is used to copy one object to another object 
     * @global type $view : used to display the variable which define for view into html file .
     * @param type $obj 
     */
    public function  copyObj($obj){
        global $view; 
        $view = $obj ;
    }
    /**
     * @desc : This function is used to give difference between 
     * to existing cache file and current time . 
     * @return type 
     */
    public function findCacheUnit() {
        $cacheTime =  filemtime(CACHE_DIR.DS.'demo.html') ;
        $currentTime = time() ; 
        $seconds = ($currentTime - $cacheTime) ; 
        $existTime = '' ;
        $unit = array(
            'day' => 24 * 60 * 60,
            'hour' => 60 * 60,
            'minute' => 60,
            'second' => 1,
        );
        $return;
        foreach ($unit as $label => $amount) {
            if ($seconds >= $amount) {
                $howMany = floor($seconds / $amount);
                $return[$label] = $howMany;
            }
        }
        return $return[CACHE_UNIT];
    }
    /**
     * @desc : This function is used to write cache file 
     * @param type $filename : name of the file 
     * @param type $content : content 
     */
    public function fileWrite($filename ,$content) {
        file_put_contents($filename.self::$cacheExt, $content);
    }
}

?>
